<?php
// +----------------------------------------------------------------------
// | 授权系统 - 客户模型
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace common\models\system;

use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;

/**
 * 客户信息模型
 * 
 * @property int $id 客户ID
 * @property string $customer_no 客户编号
 * @property string $customer_name 客户名称
 * @property string $contact_person 联系人
 * @property string $contact_phone 联系电话
 * @property string $contact_email 联系邮箱
 * @property string $company_name 公司名称
 * @property string $company_address 公司地址
 * @property int $customer_level 客户等级(1-5)
 * @property string $tags 客户标签
 * @property string $remark 备注
 * @property int $status 状态
 * @property int $created_by 创建人ID
 * @property int $updated_by 更新人ID
 * @property string $created_at 创建时间
 * @property string $updated_at 更新时间
 */
class Customer extends ActiveRecord
{
    // 客户等级常量
    const LEVEL_NORMAL = 1;      // 普通
    const LEVEL_GOOD = 2;        // 良好
    const LEVEL_EXCELLENT = 3;   // 优秀
    const LEVEL_QUALITY = 4;     // 优质
    const LEVEL_VIP = 5;         // VIP
    
    // 状态常量
    const STATUS_NORMAL = 1;
    const STATUS_DISABLED = 2;
    
    public static function tableName()
    {
        return '{{%sys_customer}}';
    }
    
    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::class,
                'value' => date('Y-m-d H:i:s'),
            ],
        ];
    }
    
    public function rules()
    {
        return [
            [['customer_no', 'customer_name'], 'required'],
            [['company_name', 'company_address', 'tags', 'remark'], 'string'],
            [['status', 'created_by', 'updated_by', 'customer_level'], 'integer'],
            [['created_at', 'updated_at'], 'safe'],
            [['customer_no'], 'string', 'max' => 50],
            [['customer_name', 'company_name'], 'string', 'max' => 255],
            [['contact_person'], 'string', 'max' => 100],
            [['contact_phone'], 'string', 'max' => 20],
            [['contact_email'], 'email'],
            [['customer_no'], 'unique'],
            [['customer_level'], 'in', 'range' => [1, 2, 3, 4, 5]],
        ];
    }
    
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'customer_no' => '客户编号',
            'customer_name' => '客户名称',
            'contact_person' => '联系人',
            'contact_phone' => '联系电话',
            'contact_email' => '联系邮箱',
            'company_name' => '公司名称',
            'company_address' => '公司地址',
            'customer_level' => '客户等级',
            'tags' => '客户标签',
            'remark' => '备注',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }
    
    /**
     * 关联授权列表
     */
    public function getLicenses()
    {
        return $this->hasMany(License::class, ['customer_id' => 'id']);
    }
}

